package com.fuda.trade.ui.holdings

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fuda.trade.MyApplication
import com.fuda.trade.ZenohManager
import com.fuda.trade.model.FudaThing
import com.fuda.trade.model.FudaThingId
import com.fuda.trade.model.TradeHolding
import com.fuda.trade.model.TradeHoldingData
import com.fuda.trade.ui.strategies.StrategiesViewModel
import io.zenoh.sample.Sample
import kotlinx.coroutines.launch

class HoldingsViewModel : ViewModel() {

    companion object {
        val TAG: String = HoldingsViewModel::class.java.simpleName
    }

    private val _holdings = MutableLiveData<List<TradeHolding>>()
    val holdings: LiveData<List<TradeHolding>>
        get() = _holdings

    init {
        refreshData()
    }


    private fun updateHoldings(holdingList: List<TradeHolding>) {
        _holdings.postValue(holdingList)

    }

    fun refreshData() {
        this.viewModelScope.launch {
            // Fetch the latest data from the server
            fetchLatestHoldingsFromServer { newCommands ->
                updateHoldings(newCommands)
            }
        }
    }

    private fun fetchLatestHoldingsFromServer(onSuccess: (List<TradeHolding>) -> Unit) {
        ZenohManager.holdingsSelector?.let {
            MyApplication.zenohManager.select(it,
                object: ZenohManager.ZenohCallback{
                    override fun onSuccess(sample: Sample) {
                        val payload = sample.value.toString()
                        Log.d(TAG, "viewModel got:  $payload" )
                        val holdings = TradeHolding.deserializeList(payload)
                        onSuccess(holdings)
                    }

                    override fun onError(error: String) {
                        Log.e(StrategiesViewModel.TAG, error)

                        // Simulate fetching data from the server
                        // Replace this with your actual network call
                        val holdings = listOf(
                            TradeHolding(
                                id= FudaThing("trade_holding", FudaThingId( TradeHolding.Companion.TRADE_HOLDING_ID_MONEY_CNY)),
                                holdingId = TradeHolding.Companion.TRADE_HOLDING_ID_MONEY_CNY,
                                data=TradeHoldingData.Money(currency = "CNY", total = 800.0, available = 123.0, assetTotal = 123.0, toadyProfit = 88.0, holdingProfit = 888.0)),
                            TradeHolding(
                                id= FudaThing("trade_holding", FudaThingId( TradeHolding.Companion.TRADE_HOLDING_ID_MONEY_HKD)),
                                holdingId = TradeHolding.Companion.TRADE_HOLDING_ID_MONEY_HKD,
                                data=TradeHoldingData.Money(currency = "HKD", total = 900.0, available = 123.0, assetTotal = 123.0, toadyProfit = 88.0, holdingProfit = 888.0)),
                            TradeHolding(
                                id= FudaThing("trade_holding", FudaThingId( TradeHolding.Companion.TRADE_HOLDING_ID_MONEY_USD)),
                                holdingId = TradeHolding.Companion.TRADE_HOLDING_ID_MONEY_USD,
                                data=TradeHoldingData.Money(currency = "USD", total = 600.0, available = 123.0, assetTotal = 123.0, toadyProfit = 88.0, holdingProfit = 888.0)),
                            // Other new holdings...
                            TradeHolding(
                                id= FudaThing("trade_holding", FudaThingId("SH.STK.600519")),
                                holdingId = "SH.STK.600519",
                                data= TradeHoldingData.Share( securityName = "贵州茅台19", profitLossAmount = 88.0, holdingPercentage = 33.0, profitLossPercentage = 88.0, marketValue = 888.0, currentPrice = 88.0, costBasis = -22.0, sharesHeld = 888, availableShares = 666, desc = "xxxx")),
                            TradeHolding(
                                id= FudaThing("trade_holding", FudaThingId( "SH.STK.600518")),
                                holdingId = "SH.STK.600518",
                                data= TradeHoldingData.Share( securityName = "贵州茅台18", profitLossAmount = 88.0, holdingPercentage = 33.0, profitLossPercentage = 88.0, marketValue = 888.0, currentPrice = 88.0, costBasis = -22.0, sharesHeld = 888, availableShares = 666, desc = "xxxx")),
                            TradeHolding(
                                id= FudaThing("trade_holding", FudaThingId("SH.STK.600517")),
                                holdingId = "SH.STK.600517",
                                data= TradeHoldingData.Share( securityName = "贵州茅台17", profitLossAmount = 88.0, holdingPercentage = 33.0, profitLossPercentage = 88.0, marketValue = 888.0, currentPrice = 88.0, costBasis = -22.0, sharesHeld = 888, availableShares = 666, desc = "xxxx")),
                            TradeHolding(
                                id= FudaThing("trade_holding", FudaThingId( "SH.STK.600516")),
                                holdingId = "SH.STK.600516",
                                data= TradeHoldingData.Share( securityName = "贵州茅台16", profitLossAmount = 88.0, holdingPercentage = 33.0, profitLossPercentage = 88.0, marketValue = 888.0, currentPrice = 88.0, costBasis = -22.0, sharesHeld = 888, availableShares = 666, desc = "xxxx")),
                            TradeHolding(
                                id= FudaThing("trade_holding", FudaThingId("SH.STK.600515")),
                                holdingId = "SH.STK.600515",
                                data= TradeHoldingData.Share( securityName = "贵州茅台15", profitLossAmount = 88.0, holdingPercentage = 33.0, profitLossPercentage = 88.0, marketValue = 888.0, currentPrice = 88.0, costBasis = -22.0, sharesHeld = 888, availableShares = 666, desc = "xxxx")),
                            TradeHolding(
                                id= FudaThing("trade_holding", FudaThingId( "SH.STK.600514")),
                                holdingId = "SH.STK.600514",
                                data= TradeHoldingData.Share( securityName = "贵州茅台14", profitLossAmount = 88.0, holdingPercentage = 33.0, profitLossPercentage = 88.0, marketValue = 888.0, currentPrice = 88.0, costBasis = -22.0, sharesHeld = 888, availableShares = 666, desc = "xxxx")),
                            TradeHolding(
                                id= FudaThing("trade_holding", FudaThingId( "SH.STK.600513")),
                                holdingId = "SH.STK.600513",
                                data= TradeHoldingData.Share( securityName = "贵州茅台13", profitLossAmount = 88.0, holdingPercentage = 33.0, profitLossPercentage = 88.0, marketValue = 888.0, currentPrice = 88.0, costBasis = -22.0, sharesHeld = 888, availableShares = 666, desc = "xxxx")),
                            TradeHolding(
                                id= FudaThing("trade_holding", FudaThingId( "SH.STK.600512")),
                                holdingId = "SH.STK.600512",
                                data= TradeHoldingData.Share( securityName = "贵州茅台12", profitLossAmount = 88.0, holdingPercentage = 33.0, profitLossPercentage = 88.0, marketValue = 888.0, currentPrice = 88.0, costBasis = -22.0, sharesHeld = 888, availableShares = 666, desc = "xxxx")),
                            TradeHolding(
                                id= FudaThing("trade_holding", FudaThingId("SH.STK.600511")),
                                holdingId = "SH.STK.600511",
                                data= TradeHoldingData.Share( securityName = "贵州茅台11", profitLossAmount = 88.0, holdingPercentage = 33.0, profitLossPercentage = 88.0, marketValue = 888.0, currentPrice = 88.0, costBasis = -22.0, sharesHeld = 888, availableShares = 666, desc = "xxxx")),
                        )

                        onSuccess(holdings)
                    }
                }
            )
        }
    }

}